import java.util.LinkedList;
import java.util.Queue;

public class MyStack {
    public Queue<Integer> sq1;
    public Queue<Integer> sq2;
    public int size=0;
    public MyStack() {
        sq1=new LinkedList<>();
        sq2=new LinkedList<>();;
    }

    public void push(int x) {
        if(!sq1.isEmpty()){
            sq1.offer(x);
        }
        if(!sq2.isEmpty()){
            sq2.offer(x);
        }
        if(sq1.isEmpty()&&sq2.isEmpty()){
            sq1.offer(x);
        }
        size++;
    }

    public int pop() {
        if(!sq1.isEmpty()){
            for (int i=0;i<size-1;i++){
                int x= sq1.poll();
                sq2.offer(x);
            }
            size--;
            return sq1.poll();
        }
        else if(!sq2.isEmpty()){
            for (int i=0;i<size-1;i++){
                int x= sq2.poll();
                sq1.offer(x);
            }
        }
        size--;

        return sq2.poll();
    }

    public int top() {
        int x=-1;
        if(!sq1.isEmpty()){
            for (int i=0;i<size;i++){
                x= sq1.poll();
                sq2.offer(x);
            }

        }
        else if(!sq2.isEmpty()){
            for (int i=0;i<size;i++){
                x= sq2.poll();
                sq1.offer(x);
            }
        }
        return x;
    }

    public boolean empty() {
        return sq1.isEmpty()&&sq2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

